package com.sky.service.impl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.BaseException;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description TODO
 * @Author itheima
 * @Date 2023-07-10 21:52
 */
@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    /**
     * 添加新菜品
     * @param dto
     */
    @Override
    @Transactional
    //一对多的添加, 一的表添加,再根据一添加的数据返回的主键,然后存入实体类再添加多的表
    public void getadd(DishDTO dto) {
        //创建菜品表的实体类对象
        Dish dish = new Dish();
        //将数据拷贝给实体类对象
        BeanUtils.copyProperties(dto,dish);
        //添加
        dishMapper.getadd(dish);
        //得到与口味表关联的自增id(主键回返)
        Long id = dish.getId();
        //通过菜品表DTO 类中的常量口味集合flavors
        List<DishFlavor> flavors = dto.getFlavors();
        //判断集合是否有数据
        if (flavors!=null||flavors.size()>0) {
            //遍历集合取出集合存储的口味表的实体类对象
            flavors.forEach(dishFlavor ->
                    //将回返的主键放入实体类
                    dishFlavor.setDishId(id)

            );
            //添加口味表实体类添加
            dishFlavorMapper.insertFlavor(flavors);
        }else {
            throw  new BaseException("请添加口味信息");
        }
    }

    /**
     * 分页查询相关接口
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        //因为  要返回的数据有菜品口味名称 是菜品实体类没有的变量 所以用创建DishVO来封装
         Page<DishVO> page= dishMapper.page(dishPageQueryDTO);
         return  new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 批量删除
     * @param ids
     */
    @Transactional
    @Override
    public void deletedish(List<Long> ids) {//删除的话一对多 一个多都要删除
        //查询与菜品表起售的总数
       Integer coem= dishMapper.getbystatus(ids);
       //判断 当com>0时说明要删除的菜品有是起售状态
       if (coem>0){
           //抛出异常
           throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
       }
       //得到要删除的菜品与套餐表关联总数
        Integer integer= setmealDishMapper.getbycont(ids);
       //判端  当nteger>0  要删除的菜品与套餐表存在关联关系
       if (integer>0){
           throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
       }
       //批量删除菜品
            dishMapper.deleteid(ids);
       //批量删除菜品关联的口味
           dishFlavorMapper.delete(ids);
    }

    /**
     * 根据Id查询
     * @param id
     * @return
     */
    @Override
    public DishVO getbyid(Long id) {
         DishVO  dishVO= dishMapper.getbyid(id);
        List<DishFlavor> flavors = dishFlavorMapper.getByDishId(id);
        dishVO.setFlavors(flavors);
        return dishVO;
    }

    /**
     * 修改菜品
     * @param dto
     */
    @Override
    @Transactional
    public void update(DishDTO dto) {//修改 一对多 一先修改 多的先删除 在添加
        //创建菜品的实体类
        Dish dish = new Dish();
        //将数据拷贝给实体类
        BeanUtils.copyProperties(dto,dish);
        //修改菜品
        dishMapper.update(dish);
        //删除口味表的数据
        dishFlavorMapper.deleteid(dto.getId());
        List<DishFlavor> flavors = dto.getFlavors();
        if (flavors!=null&&flavors.size()>0) {
            flavors.forEach(dishFlavor ->
                    dishFlavor.setDishId(dto.getId())
                    );
        }
        dishFlavorMapper.insertFlavor(flavors);
    }

    /**
     * 修改售卖状态接口
     * @param status
     * @param id
     */
    @Override
    public void updatestatus(Integer status, Long id) {
        Dish build = Dish.builder()
                .id(id)
                .status(status)
                .build();

        dishMapper.update(build);
    }
    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> list(Long categoryId) {
        Dish dish = Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        return dishMapper.list(dish);
    }
    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    @Override
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}
